package com.fengwk.support.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.fengwk.support.constant.SupportConstant;
import com.fengwk.support.exception.BaseException;

/**
 * XML操作工具栏
 */

//		xPath语法:
//  	/	单层路径
//  	//  之前所有忽略
//  	*   随意元素
//  	/*/*/*	随意第三层元素
//		/*/*/*[1]	随意第三层第一个元素
// 		/AAA/BBB[1]   第一个子元素
//  	/AAA/BBB[last()]   最后一个
//  	@	后边加属性
//		@别名		查询为属性
//		//*[@别名]	查询为节点

public class Dom4jUtils {
	
	private static XMLWriter xmlWriter;// xml写入工具对象
	
	private Dom4jUtils() {
		throw new BaseException("can not instance " + getClass());
	}
	
	static {
		//格式化输出,防止中文乱码
		OutputFormat outputFormat = OutputFormat.createPrettyPrint();
		outputFormat.setEncoding(SupportConstant.DEFAULT_CHARSET);
		try {
			xmlWriter = new XMLWriter(outputFormat);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 加载xml文档
	 * @param path
	 * @return
	 */
	public static Document load(String path) {
		return load(IoUtils.buildReader(path));
	}
	
	/**
	 * 加载xml文档
	 * @param file
	 * @return
	 */
	public static Document load(File file) {
		return load(IoUtils.buildReader(file));
	}
	
	/**
	 * 加载xml文档
	 * @param is
	 * @return
	 */
	public static Document load(InputStream is) {
		return load(IoUtils.toReader(is));
	}
	
	/**
	 * 加载xml文档
	 * @param reader
	 * @return
	 */
	public static Document load(Reader reader) {
		Document document = null;
		//得到解析器
		SAXReader saxReader = new SAXReader();
		//解析为document对象
		try {
			document = saxReader.read(reader);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return document;
	}
	
	/**
	 * 写入xml文档
	 * @param document dom4j document
	 * @param writer 文档输出writer
	 * @return
	 * @throws IOException
	 */
	public static void write(Document document, Writer writer) throws IOException {
		if(document == null){
			throw new NullPointerException("document is null");
		}
		xmlWriter.setWriter(writer);
		xmlWriter.write(document);
		IoUtils.close(writer);
	}
	
	/**
	 * 写入xml文档
	 * @param document
	 * @param path
	 * @throws IOException
	 */
	public static void write(Document document, String path) throws IOException {
		write(document, IoUtils.buildWriter(path));
	}
	
	/**
	 * 写入xml文档
	 * @param document
	 * @param file
	 * @throws IOException
	 */
	public static void write(Document document, File file) throws IOException {
		write(document, IoUtils.buildWriter(file));
	}
	
	/**
	 * 写入xml文档
	 * @param document
	 * @param os
	 * @throws IOException
	 */
	public static void write(Document document, OutputStream os) throws IOException {
		write(document, IoUtils.toWriter(os));
	}
	
	/**
	 * 节点打印
	 * @param nodeList
	 */
	public static <N extends Node> void print(List<N> nodeList) {
		for(N node : nodeList) {
			System.out.println(node.asXML());
		}
	}
	
}